package com.yc.boot.log.business;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yc.boot.log.config.LogCacheConfig;
import com.yc.boot.log.entity.DataLog;
import com.yc.boot.log.entity.WorkLog;
import com.yc.boot.log.model.find.FindDataLogPage;
import com.yc.boot.log.model.find.FindWorkLogPage;
import com.yc.boot.log.model.save.SaveDataLog;
import com.yc.boot.log.model.update.UpdateLogger;
import com.yc.boot.log.service.DataLogService;
import com.yc.boot.log.service.WorkLogService;
import com.yc.boot.sys.business.SysUtilBusiness;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

import static com.yc.boot.common.constant.BaseConstant.NINETY_NINE;

/**
 * @author 杨智杰
 * @since 2021/8/11 11:28
 */
@Slf4j
@Service
public class LoggerBusiness {

    @Resource
    private SysUtilBusiness sysUtilBusiness;

    @Resource
    private DataLogService dataLogService;

    @Resource
    private WorkLogService workLogService;

    /**
     * 新增
     *
     * @return
     */
    public Boolean save(SaveDataLog param) {
        DataLog dataLog = Convert.convert(DataLog.class, param);
        return dataLogService.save(dataLog.setCreateTime(DateUtil.now()));
    }

    /**
     * 修改
     *
     * @param param
     * @return
     */
    public Boolean update(UpdateLogger param) {
        return dataLogService.updateById(Convert.convert(DataLog.class, param));
    }

    /**
     * 分页查询
     *
     * @param param
     * @return
     */
    public IPage<DataLog> page(FindDataLogPage param) {
        Page<DataLog> page = dataLogService.page(new Page<>(param.getCurrent(), param.getSize()),
                new QueryWrapper<DataLog>().lambda().eq(ObjectUtil.isNotNull(param.getId()), DataLog::getId, param.getId())
                        .like(StrUtil.isNotBlank(param.getMethod()), DataLog::getMethod, param.getMethod())
                        .like(StrUtil.isNotBlank(param.getHeaders()), DataLog::getHeaders, param.getHeaders())
                        .like(StrUtil.isNotBlank(param.getRequestBody()), DataLog::getRequestBody, param.getRequestBody())
                        .like(StrUtil.isNotBlank(param.getRequestURI()), DataLog::getRequestURI, param.getRequestURI())
                        .or(StrUtil.isNotBlank(param.getLoginName()),
                                i -> i.like(DataLog::getHeaders, StrUtil.DOT + SecureUtil.md5(param.getLoginName()) + StrUtil.DOT)
                                        .or().like(DataLog::getRequestBody, "\"loginName\": \"" + param.getLoginName() + "\""))
                        .ge(StrUtil.isNotBlank(param.getBeginTime()), DataLog::getCreateTime, param.getBeginTime())
                        .le(StrUtil.isNotBlank(param.getEndTime()), DataLog::getCreateTime, param.getEndTime())
                        .orderByDesc(DataLog::getCreateTime));
        page.getRecords().stream().forEach(dataLog -> dataLog.setResponseBody("******").setHeaders("******"));
        return page;
    }

    public IPage<WorkLog> page(FindWorkLogPage param) {
        return workLogService.page(new Page<>(param.getCurrent(), param.getSize()),
                new QueryWrapper<WorkLog>().lambda().eq(ObjectUtil.isNotNull(param.getId()), WorkLog::getId, param.getId())
                        .eq(ObjectUtil.isNotNull(param.getCreateBy()), WorkLog::getCreateBy, param.getCreateBy())
                        .like(StrUtil.isNotBlank(param.getParam()), WorkLog::getParam, param.getParam())
                        .ge(StrUtil.isNotBlank(param.getBeginTime()), WorkLog::getCreateTime, param.getBeginTime())
                        .le(StrUtil.isNotBlank(param.getEndTime()), WorkLog::getCreateTime, param.getEndTime())
                        .eq(param.getLogin(), WorkLog::getCreateBy, sysUtilBusiness.getLoginUser().getId()));
    }

    /**
     * 删除
     *
     * @param param
     * @return
     */
    public Boolean delete(List<Long> param) {
        return dataLogService.removeByIds(param);
    }

    /**
     * 添加数据日志缓存
     *
     * @param param
     */
    public void setCaChe(SaveDataLog param) {
        LogCacheConfig.DATA_LOG_CACHE
                .put(SecureUtil.md5(RandomUtil.randomString(NINETY_NINE) + System.currentTimeMillis()), param);
    }

    /**
     * 添加业务日志缓存
     *
     * @param param
     */
    public void setCache(WorkLog param) {
        LogCacheConfig.WORK_LOG_CACHE
                .put(SecureUtil.md5(RandomUtil.randomString(NINETY_NINE) + System.currentTimeMillis()), param);
    }

    /**
     * 根据Id查询
     *
     * @param id
     * @return
     */
    public DataLog getDataLogById(Long id) {
        return dataLogService.getById(id);
    }
}
